package org.ielse.frame;

import org.ielse.frame.model.AppSettings;
import org.ielse.frame.t.volley.BitmapCache;
import org.litepal.LitePalApplication;
import org.litepal.crud.DataSupport;

import android.content.Context;
import android.content.SharedPreferences;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class App extends LitePalApplication {

	private AppSettings appSettings;

	private static App instance;
	private RequestQueue requestQueue;
	private ImageLoader imageLoader;
	private BitmapCache bitmapCache;
	private SharedPreferences sharedPreferences;

	@Override public void onCreate() {
		super.onCreate();
		instance = this;
		requestQueue = Volley.newRequestQueue(instance);
		bitmapCache = new BitmapCache();
		imageLoader = new ImageLoader(requestQueue, bitmapCache);
		sharedPreferences = getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);

		appSettings = DataSupport.findFirst(AppSettings.class);
		if (appSettings == null) {
			appSettings = new AppSettings();
			appSettings.save();
		}

		// CrashHandler.setDefaultUncaughtExceptionHandler(this);
	}

	public static synchronized App i() {
		return instance;
	}

	public RequestQueue getRequestQueue() {
		return requestQueue;
	}

	public ImageLoader getImageLoader() {
		return imageLoader;
	}

	public BitmapCache getBitmapCache() {
		return bitmapCache;
	}

	public SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}

	@Override public void onTerminate() {
		super.onTerminate();
	}

	@Override public void onLowMemory() {
		super.onLowMemory();
	}

	public AppSettings getAppSettings() {
		return appSettings;
	}

	public void setAppSettings(AppSettings appSettings) {
		this.appSettings = appSettings;
		appSettings.update(appSettings.getId());
	}
}
